package jp.co.toshiba.ffsa.action.csas;

import java.util.ArrayList;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import jp.co.toshiba.ffsa.action.common.CommonAction;
import jp.co.toshiba.ffsa.dto.LoginDto;
import jp.co.toshiba.ffsa.form.csas.Csas503Form;
import jp.co.toshiba.ffsa.service.common.ConstantValue;
import jp.co.toshiba.ffsa.service.csas.Csas503Service;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.util.ActionMessagesUtil;
import org.seasar.struts.util.MessageResourcesUtil;
import org.seasar.struts.util.RequestUtil;

/**
 * (パスワード忘れ)パスワード再登録
 * @author npc
 */
public class Csas503Action extends CommonAction{

    /** 画面ID */
    public String screenID = "CSAS503";

    /** ActionForm */
    @ActionForm
    @Resource
    protected Csas503Form csas503Form;

    /** Service */
    @Resource
    protected Csas503Service csas503Service;

    /** Session */
    @Resource
    protected HttpSession session;

    /**
     * 初期処理
     * @return 画面遷移先
     * @throws Exception エラー
     */
    @Execute(validator = false)
    public String index() throws Exception{
        try{
            // リファラ判定
            if(this.judgeRefferer()){
                // セッションタイムアウト判定
                if(this.judgeSession()){
                    return "/csas/csas503.jsp";
                }else{
                    return this.topTransition;
                }
            }else{
                return this.topTransition;
            }
        }catch(Exception e){
            this.errorLogWriter(e, "index", this.screenID);
            throw e;
        }
    }

    /**
     * 再登録処理
     * @param strUserID ユーザーID
     * @return 画面遷移先
     * @throws Exception
     */
    @Execute(validator=true, input="/csas/csas503/error")
    public String updateNewPassword() throws Exception{
        // 遷移先
        String transition = "/csas/csas503.jsp";
        String result = "";
        try {
            // リファラ判定
            if(this.judgeRefferer()){
                // セッションタイムアウト判定
                if(this.judgeSession()){
                    String strUserID = ((LoginDto)RequestUtil.getRequest().getSession().getAttribute("login_dto")).getUserID();
                    // 登録処理
                    csas503Service = new Csas503Service();
                    csas503Service.setNewPassword(this.csas503Form.newPassword);
                    csas503Service.setUserId(strUserID);

                    boolean bFlag = csas503Service.checkInputPassword(strUserID);

                    if(!bFlag){
                        //エラー表示
                        ActionMessages messages = new ActionMessages();
                        messages.add("updateNewPasswordFailed", new ActionMessage(csas503Service.getErrorCode(), true));
                        ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);
                        result = ConstantValue.fail;
                        transition = "/csas/csas503.jsp";
                    }else{
                        // 更新処理
                        boolean successFlag = csas503Service.updateNewPasswordByUserId(strUserID);
                        if(!successFlag){
                            //エラー表示
                            ActionMessages messages = new ActionMessages();
                            messages.add("updateNewPasswordFailed", new ActionMessage("errors.password", true));
                            ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);
                            result = ConstantValue.fail;
                            transition = "/cser/cser101.jsp";
                        }else{
                            boolean sendFlag = csas503Service.mailSend();
                            if(!sendFlag){
                                //エラー表示
                                ActionMessages messages = new ActionMessages();
                                messages.add("ActionMessagesFailed", new ActionMessage("errors.password", true));
                                ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);
                                result = ConstantValue.fail;
                                transition = "/cser/cser101.jsp";
                            }else{
                                // 完了画面に移動
                                result = ConstantValue.success;
                                transition = "/csas/csas504.jsp";
                            }
                        }
                    }

                    if(ConstantValue.success.equals(result)){
                        // 操作ログ記述
                        ArrayList<ArrayList<String>> parameters = new ArrayList<ArrayList<String>>();
                        ArrayList<String> parameterNames = new ArrayList<String>();
                        ArrayList<String> values = new ArrayList<String>();
                        parameterNames.add(MessageResourcesUtil.getMessage("new_password"));
                        values.add("-");
                        parameterNames.add(MessageResourcesUtil.getMessage("Reenter_new_password"));
                        values.add("-");
                        parameters.add(parameterNames);
                        parameters.add(values);
                        this.entryOperationHistory(this.screenID, "submit", parameters, result);
                    }

                    return transition;

                }else{
                    return this.topTransition;
                }
            }else{
                return this.topTransition;
            }
        } catch (Exception e) {
            this.errorLogWriter(e, "updateNewPassword", this.screenID);
            throw e;
        }
    }

    /**
     * バリデーションエラー処理
     * @return 遷移先
     * @throws Exception
     */
    @Execute(validator=false)
    public String error() throws Exception{
        return index();
    }
}